package com.qs.main.ui;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.android.arouter.launcher.ARouter;
import com.qs.base.router.RouterActivityPath;
import com.qs.main.R;

import java.lang.ref.WeakReference;

/**
 * Created by Walter on 2019/6/14.
 * shiys
 */

public class DialogHomeUtils extends Dialog implements DialogInterface.OnCancelListener {
    private WeakReference<Context> mContext = new WeakReference<>(null);
    private volatile static DialogHomeUtils sDialog;


    public DialogHomeUtils(@NonNull Context context , OnClick onClickListener) {
        super(context, R.style.CustomProgressDialog);
        init(context ,onClickListener);
    }

    private void init(final Context context , final OnClick onClickListener) {
        if (mContext.get() == null) {
            mContext = new WeakReference<>(context);
            @SuppressLint("InflateParams")
            View view = LayoutInflater.from(mContext.get()).inflate(R.layout.layout_private_dialog, null);
            LinearLayout ll_box = view.findViewById(R.id.ll_box);
            TextView tv_user = view.findViewById(R.id.tv_user);
            TextView tv_private = view.findViewById(R.id.tv_private);
            final CheckBox box = view.findViewById(R.id.box);

            TextView tv_go = view.findViewById(R.id.tv_go);
            TextView tv_finish = view.findViewById(R.id.tv_finish);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            addContentView(view, lp);
            setCancelable(false);
            setCanceledOnTouchOutside(false);
            setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    return true;
                }
            });
            setOnCancelListener(this);


            tv_finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            tv_go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(box.isChecked()){
                        onClickListener.goIntent();
                    }else{
                        Toast.makeText(context ,"已阅读并接受，请勾选用户协议与隐私政策", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            tv_user.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build(RouterActivityPath.User.TreatyActivity).navigation();
                }
            });

            tv_private.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build(RouterActivityPath.User.AGREEMENT_ACTIVITY).navigation();

                }
            });

            ll_box.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(box.isChecked()){
                        box.setChecked(false);
                    }else{
                        box.setChecked(true);
                    }
                }
            });

        }



    }

    @Override
    public void onCancel(DialogInterface dialog) {

    }

//    public static synchronized void setTitleMessage(String message) {
//        if (tvMessage != null) {
//            tvMessage.setText(message);
//        }
//    }


//    public static synchronized void showLoading(Context context) {
//        showLoading(context, "loading...");
//    }

    public static synchronized void showLoading(Context context, OnClick message) {
        showLoading(context, message, true);
    }

    public static synchronized void showLoading(Context context, OnClick message, boolean cancelable) {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }

        if (context == null || !(context instanceof Activity)) {
            return;
        }
        sDialog = new DialogHomeUtils(context ,message);
        sDialog.setCancelable(cancelable);

        if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) {
            sDialog.show();
        }
    }

    public static synchronized void stopLoading() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
//        if(tvMessage != null){
//            tvMessage = null ;
//        }
        sDialog = null;
    }

    public interface OnClick{

        void goIntent();

    }

}
